#include "statisticwidget.h"
#include "ui_statisticwidget.h"

///
/// \brief StatisticWidget::StatisticWidget
/// \param parent
///
StatisticWidget::StatisticWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::StatisticWidget)
    ,_numberOfPolls(0)
    ,_validSlaveResponses(0)
    ,_pollState(static_cast<PollState>(-1))
{
    ui->setupUi(this);
    setPollState(PollState::Off);
}

///
/// \brief StatisticWidget::~StatisticWidget
///
StatisticWidget::~StatisticWidget()
{
    delete ui;
}

///
/// \brief StatisticWidget::changeEvent
/// \param event
///
void StatisticWidget::changeEvent(QEvent* event)
{
    if (event->type() == QEvent::LanguageChange)
    {
        ui->retranslateUi(this);
        setPollState(_pollState);
    }

    QWidget::changeEvent(event);
}

///
/// \brief StatisticWidget::increaseNumberOfPolls
///
void StatisticWidget::increaseNumberOfPolls()
{
    _numberOfPolls++;
    updateStatistic();

    emit numberOfPollsChanged(_numberOfPolls);
}

///
/// \brief StatisticWidget::increaseValidSlaveResponses
///
void StatisticWidget::increaseValidSlaveResponses()
{
   _validSlaveResponses++;
   updateStatistic();

   emit validSlaveResposesChanged(_validSlaveResponses);
}

///
/// \brief StatisticWidget::resetCtrls
///
void StatisticWidget::resetCtrs()
{
    _numberOfPolls = 0;
    _validSlaveResponses = 0;

    updateStatistic();

    emit numberOfPollsChanged(_numberOfPolls);
    emit validSlaveResposesChanged(_validSlaveResponses);
}

///
/// \brief StatisticWidget::pollState
/// \return
///
PollState StatisticWidget::pollState() const
{
    return _pollState;
}

///
/// \brief StatisticWidget::setPollState
/// \param state
///
void StatisticWidget::setPollState(PollState state)
{
    if(_pollState == state)
        return;

    _pollState = state;
    switch (state)
    {
        case PollState::Off:
            ui->pushButtonPause->setEnabled(false);
            ui->pushButtonPause->setText(tr("Pause Polling"));
        break;
        case PollState::Running:
            ui->pushButtonPause->setEnabled(true);
            ui->pushButtonPause->setText(tr("Pause Polling"));
        break;
        case PollState::Paused:
            ui->pushButtonPause->setEnabled(true);
            ui->pushButtonPause->setText(tr("Resume Polling"));
        break;
    }
    emit pollStateChanged(pollState());
}

///
/// \brief StatisticWidget::on_pushButtonResetCtrs_clicked
///
void StatisticWidget::on_pushButtonResetCtrs_clicked()
{
    resetCtrs();
    emit ctrsReseted();
}

///
/// \brief StatisticWidget::on_pushButtonPause_clicked
///
void StatisticWidget::on_pushButtonPause_clicked()
{
    switch (_pollState) {
    case PollState::Off: break;
    case PollState::Running: setPollState(PollState::Paused); break;
    case PollState::Paused: setPollState(PollState::Running); break;
    }

    emit pollStateChanged(pollState());
}

///
/// \brief StatisticWidget::updateStatistic
///
void StatisticWidget::updateStatistic()
{
    ui->labelNumberOfPolls->setText(QString(tr("Number of Polls: %1")).arg(_numberOfPolls));
    ui->labelValidSlaveResponses->setText(QString(tr("Valid Slave Responses: %1")).arg(_validSlaveResponses));
}
